import { Decrypt } from "@/libs/crypto";
import { user, tenant } from "@/api/index";
export default {
  data () {
    return {
      isShowMsgCode: true,
      sendDisabled: true,
      showTime: null,
      pictureShow: false,
      imgLists: [],
      verify: "",
      frequency: ""
    };
  },

  methods: {
    handleSend () {
      // this.sendOutTime()
      // this.pictureShow = true
      this.picture();
    },
    success () {
      this.pictureShow = false;
      this.loginCodeSend(this.ruleForm);
      this.sendOutTime();
    },
    picture () {
      user.graphicalValidate().then((res) => {
        // let { requestId, url, imageName, beginXPoint, beginYPoint, endXPoint, endYPoint } = res.data
        let { iv, imageUrl, imageName, beginX, beginY, endX, endY } = res.data;
        this.ruleForm.requestId = iv;
        const imtUrl = Decrypt(imageUrl, iv, iv) + Decrypt(imageName, iv, iv);
        this.imgLists.push(imtUrl);
        const [o1, o2, o3, o4, o5] = [
          Decrypt(beginX, iv, iv),
          Decrypt(beginY, iv, iv),
          Decrypt(endX, iv, iv),
          Decrypt(endY, iv, iv),
          Decrypt(imageUrl, iv, iv)
        ];
        const verifyList = o5 + iv + o1 + o2 + o3 + o4;
        this.ruleForm.verify = this.$md5(verifyList);
        this.pictureShow = true;
      });
    },
    sendOutTime () {
      this.isShowMsgCode = false;
      this.showTime = 60;
      const authTimeTimer = setInterval(() => {
        this.showTime--;
        if (this.showTime <= 0) {
          this.isShowMsgCode = true;
          clearInterval(authTimeTimer);
        }
      }, 1000);
    },
    async loginCodeSend (ruleForm) {
      let { phoneNum, verify, requestId } = ruleForm;
      let postData = {
        phoneNum: phoneNum,
        requestId: requestId,
        verify: verify
      };
      let data = await tenant.passwordCode(postData);
      let { code } = data;
      this.frequency = data?.data || null;
      this.$message[code === 0 ? "success" : "error"](data.message);
    }
  }
};
